<%@page import="com.google.android.gcm.server.Result"%>
<%@page import="com.google.android.gcm.server.MulticastResult"%>
<%@page import="java.util.List"%>
<%@page import="com.google.android.gcm.server.Message"%>
<%@page import="com.google.android.gcm.server.Sender"%>
<%@page import="com.glowdayz.glowmee.dao.RegisterDAO"%>
<%@page import="com.glowdayz.glowmee.cmd.PouchCommentCMD"%>
<%@page import="com.glowdayz.glowmee.dao.PouchCommentDAO"%>
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@page import="com.glowdayz.common.DBHandler"%>
<%@page import="com.glowdayz.common.ApiHandler"%>
<%@page import="com.glowdayz.common.ApiHandler.XmlData"%>
<%@page import="java.util.ArrayList"%>
<%
	DBHandler.getInstance().connect();

	ApiHandler web = new ApiHandler(request, out, "JSPglowmee");
	
	int idRegister = web.getInt("idRegister");
	int idPouch = web.getInt("idPouch");
	String comment = web.getValue("comment");
	int writer = web.getInt("writer");
	
	int insert = 0;
	
	PouchCommentCMD cmd = new PouchCommentCMD(idRegister, idPouch, comment);
	insert = PouchCommentDAO.getInstance().insertPouchComment(cmd);
	
	if (insert < 1) {
		DBHandler.getInstance().disconnect();
		web.result(null, "등록에 실패하였습니다.");
		return;
	}

	if (writer > 0) {
		String regId = RegisterDAO.getInstance().getRegId(writer);
		if (regId != null && !regId.equals("")) {
			Sender sender = new Sender("AIzaSyBfwDUfvGLZARl6E8nNSC2LVUWPM4Wnosg");
			String msg =  "내 픽에 새로운 댓글이 달렸어요 :)";
			Message message = new Message.Builder().addData("msg", msg).addData("linkType", "viewPouch").addData("linkCode", String.valueOf(idPouch)).build();

			List<String> list = new ArrayList<String>();
			list.add(regId);

			MulticastResult multiResult = sender.send(message, list, 5);

			if (multiResult != null) {
				List<Result> resultList = multiResult.getResults();
				for (Result result : resultList) {
					System.out.println(result.getMessageId());
				}
			}
		}
	}
	
	DBHandler.getInstance().disconnect();
	
	ArrayList<XmlData> list = new ArrayList<XmlData>();
	list.add(new XmlData("seq", insert));
	web.result("SUCCESS", "댓글이 등록되었습니다.", list);
%>